package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.WeiboMapper;
import cn.tedu.egweibo2.pojo.dto.WeiboDTO;
import cn.tedu.egweibo2.pojo.entity.Weibo;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import cn.tedu.egweibo2.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo2.pojo.vo.WeiboVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     * 接口地址: /v1/weibo/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @PostMapping("/insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        //打桩
        log.debug(weiboDTO.toString());
        //检验登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null)
            return 2;
        //发布微博
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreated(new Date());
        //打桩
        log.debug(weibo.toString());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }

    /**
     * 首页微博列表功能
     * 接口地址: /v1/weibo/selectIndex
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: List<WeiboVO>
     */
    @GetMapping("/selectIndex")
    public List<WeiboVO> selectIndex(){
        return weiboMapper.selectIndex();
    }

    /**
     * 微博详情页功能
     * 接口地址: /v1/weibo/selectById
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: WeiboDetailVO
     */
    @GetMapping("/selectById")
    public WeiboDetailVO selectById(Integer id){
        return weiboMapper.selectById(id);
    }
}
